/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tarifador.view;

import br.com.tarifador.controller.UsuarioRN;
import br.com.tarifador.dataModel.UsuarioRamalDataModel;
import br.com.tarifador.model.UsuarioRamal;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.HibernateException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author charles
 */
@ManagedBean
@SessionScoped
public class UsuarioRamalBean {

    private UsuarioRN usuarioRN = new UsuarioRN();
    private List<UsuarioRamal> listaUsuario;
    private UsuarioRamal usuarioSelected;
    private UsuarioRamalDataModel usuarioModel;

    public void salvaUsuarioRamal() {

        boolean salvar = false;
        try {
            salvar = usuarioRN.salvaUsuarioRamal();
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (salvar == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaUsuarioRamal();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void listaUsuarioRamal() {
        listaUsuario = new ArrayList<UsuarioRamal>();
        try {
            listaUsuario = usuarioRN.listaUsuarioRamal();

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        this.usuarioModel = new UsuarioRamalDataModel(listaUsuario);
    }

    public void alteraUsuarioRamal() {
        boolean valida = false;
        try {
            valida = usuarioRN.updateUsuarioRamal(usuarioSelected);

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
          listaUsuarioRamal();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));

        }
    }

    public void deleteUsuarioRamal() {
        boolean valida = false;
        try {
            valida = usuarioRN.deletaUsuarioRamal(usuarioSelected.getId());
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!\n " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!\n " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaUsuarioRamal();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void btnAlterarSenha() {
        RequestContext request = RequestContext.getCurrentInstance();
        try {
            if (usuarioSelected.getId() != null && usuarioSelected.getId() > 0) {
                request.addCallbackParam("sucesso", true);
            } else {
                request.addCallbackParam("sucesso", false);
            }
        } catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo!", ""));
        }
    }
   public void limpaListaUSuario() {
        if (usuarioModel != null) {
            listaUsuario.clear();
            usuarioModel = new UsuarioRamalDataModel(listaUsuario);
        }
    }

    public UsuarioRN getUsuarioRN() {
        return usuarioRN;
    }

    public void setUsuarioRN(UsuarioRN usuarioRN) {
        this.usuarioRN = usuarioRN;
    }

    public List<UsuarioRamal> getListaUsuario() {
        return listaUsuario;
    }

    public void setListaUsuario(List<UsuarioRamal> listaUsuario) {
        this.listaUsuario = listaUsuario;
    }

    public UsuarioRamal getUsuarioSelected() {
        return usuarioSelected;
    }

    public void setUsuarioSelected(UsuarioRamal usuarioSelected) {
        this.usuarioSelected = usuarioSelected;
    }

    public UsuarioRamalDataModel getUsuarioModel() {
        return usuarioModel;
    }

    public void setUsuarioModel(UsuarioRamalDataModel usuarioModel) {
        this.usuarioModel = usuarioModel;
    }
}
